Skip to content

Reduce CreateMerchantUser complexity in MerchantDomainService#1626

Merged
StuartFerguson merged 3 commits intomasterfrom
copilot/fix-cyclomatic-complexity-issue-another-one
Mar 13, 2026
Merged

Reduce CreateMerchantUser complexity in MerchantDomainService#1626
StuartFerguson merged 3 commits intomasterfrom
copilot/fix-cyclomatic-complexity-issue-another-one

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

Codacy flagged MerchantDomainService.CreateMerchantUser for cyclomatic complexity above the configured limit. This change keeps the method behavior intact while moving incidental branching into focused helpers.

  • Refactor the public workflow

    • Keep CreateMerchantUser centered on the domain flow: load aggregates, validate estate/merchant state, attach the created security user, persist the aggregate.
    • Remove request-construction and security-user lookup details from the main method.
  • Extract merchant user request construction

    • Add a private helper to build CreateUserRequest, including role resolution and claims population.
  • Extract security user provisioning

    • Add a private helper that:
      • creates the user via ISecurityServiceClient
      • retrieves the created user by email
      • returns the resolved UserDetails or the existing failure result
  • Behavior preserved

    • Existing failure paths and error messages remain unchanged.
    • Aggregate mutation and save behavior are untouched.
CreateUserRequest createUserRequest = this.BuildMerchantUserRequest(command);

Result<UserDetails> getUserResult =
    await this.CreateMerchantSecurityUser(createUserRequest, cancellationToken);

Result stateResult =
    merchantAggregate.AddSecurityUser(getUserResult.Data.UserId, command.RequestDto.EmailAddress);

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • f.feedz.io
    • Triggering command: /usr/bin/dotnet dotnet test /home/REDACTED/work/TransactionProcessor/TransactionProcessor/TransactionProcessor.BusinessLogic.Tests/TransactionProcessor.BusinessLogic.Tests.csproj --filter CreateMerchantUser -nologo (dns block)
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp (dns block)
    • Triggering command: /usr/bin/dotnet dotnet restore --no-dependencies /home/REDACTED/work/TransactionProcessor/TransactionProcessor/TransactionProcessor.sln --packages /tmp/codeql-scratch-7ae8d40573b4aa77/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-7ae8d40573b4aa77/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-7ae8d40573b4aa77/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[Complexity] Method MerchantDomainService::CreateMerchantUser has a cyclomatic complexity of 11 (limit is 8)</issue_title>
<issue_description>

Codacy ID: d9b4f5ff703dcb944075e08372c7e974

File: TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs
Line: 242
Rule: Complexity (Warning)
Commit: 5b8f3e64d731c7b181b225e9d4117fda74faa3ac

Message: Method MerchantDomainService::CreateMerchantUser has a cyclomatic complexity of 11 (limit is 8)

**Code Snippet:**
```csharp
        public async Task<Result> CreateMerchantUser(MerchantCommands.CreateMerchantUserCommand command, CancellationToken cancellationToken) 
```

</issue_description>

Comments on the Issue (you are @copilot in this section)


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Co-authored-by: StuartFerguson <16325469+StuartFerguson@users.noreply.github.com>
Copilot AI changed the title [WIP] [Complexity] Reduce cyclomatic complexity in CreateMerchantUser method Reduce CreateMerchantUser complexity in MerchantDomainService Mar 13, 2026
Copilot AI requested a review from StuartFerguson March 13, 2026 17:44
@StuartFerguson StuartFerguson marked this pull request as ready for review March 13, 2026 23:28
@StuartFerguson StuartFerguson merged commit b4e4e32 into master Mar 13, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Complexity] Method MerchantDomainService::CreateMerchantUser has a cyclomatic complexity of 11 (limit is 8)

2 participants